package org.chaiyi.template.app.demo;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.chaiyi.template.contract.demo.Demo;
import org.chaiyi.template.contract.demo.IDemoService;
import org.chaiyi.template.core.common.common.URLConstant;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(URLConstant.base + IDemoService.Contract.path)
public class DemoApi {
    private final DemoService demoService;

    /**
     * 会产生业务异常
     */
    @GetMapping("/businessError")
    public Mono<Demo> businessError() {
        log.info("【businessError】start");
        return demoService.businessError();
    }

    /**
     * 会产生系统异常
     */
    @GetMapping("/systemError")
    public Mono<Demo> systemError() {
        log.info("【businessError】start");
        return demoService.systemError();
    }


    /**
     * 设置string value到redis中
     */
    @GetMapping("/redis/test/setString")
    public Mono<Boolean> setString(@PathVariable String tenantId, @PathVariable String buCode, @RequestParam String value) {
        return demoService.setString(tenantId, buCode, value);
    }

    /**
     * 从redis中获得string value
     */
    @GetMapping("/redis/test/getString")
    public Mono<String> getString(@PathVariable String tenantId, @PathVariable String buCode) {
        return demoService.getString(tenantId, buCode);
    }

    /**
     * 实体类的缓存
     */
    @GetMapping("/redis/test/{id}")
    public Mono<Demo> findByRedis(@PathVariable String tenantId, @PathVariable String buCode, @PathVariable String id) {
        return demoService.findByRedis(tenantId, buCode, id);
    }
}
